package weibo4j.examples.oauth2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.examples.Constants;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

public class OAuth4Code {
	public static void main(String[] args) throws WeiboException, IOException {
		pub(Constants.access_token);
	}
	public static String logIn() throws Exception{
		String param1 = "";
		String param2 = "";
		String access_token ="";
		Oauth oauth = new Oauth();
		BareBonesBrowserLaunch.openURL(oauth.authorize("code", param1, param2));
		System.out.println(oauth.authorize("code", param1, param2));
		System.out.print("Hit enter when it's done.[Enter]:");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String code = br.readLine();
		Log.logInfo("code: " + code);
		try {
			access_token = oauth.getAccessTokenByCode(code).toString();
			System.out.println();
		} catch (WeiboException e) {
			if (401 == e.getStatusCode()) {
				Log.logInfo("Unable to get the access token.");
			} else {
				e.printStackTrace();
			}
		}
		return access_token;
	}
	public static void pub(String access_token) {
		//String access_token = "2.00lBvA1CxikhaBfbc0d2a0c10FWttI";
		String statuses = "此条微博来自:《鬼泣5》官方CG预告震撼公布 拯救苍生";
		Timeline tm = new Timeline();
		tm.client.setToken(access_token);
		try {
			Status status = tm.UpdateStatus(statuses);
			Log.logInfo(status.toString());
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}
}
